// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.

import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App' 
import routes from './router' //路由数据
import ElementUI from 'element-ui'
import axios from 'axios' // 处理http请求

import store from './store' // 状态管理
import 'element-ui/lib/theme-default/index.css'

import moment from 'moment'//时间过滤器
Vue.prototype.moment = moment
import '../static/js/time.js';

Vue.config.productionTip = false



require('./assets/common.scss')
import Mint from 'mint-ui'
import 'mint-ui/lib/style.css'//MintUI

import VeeValidate from 'vee-validate'
Vue.use(VeeValidate)

// import verify from "vue-verify-plugin";
// Vue.use(verify,{
//    blur:true
// });
// import VIscroll from 'viscroll';//滚动
Vue.use(VueRouter)
Vue.use(ElementUI)

//接口默认地址
axios.defaults.baseURL = 'http://59.49.12.73:8088';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';



// export路由
export const router = new VueRouter({
  routes  
});

axios.interceptors.request.use(
  config => { 
    if (localStorage.getItem('tokendata')) {
      let Storagetoken = JSON.parse(localStorage.getItem('tokendata')).token;
      let StorageisExpired= JSON.parse(localStorage.getItem('tokendata')).isExpired;
      let Storagesuccess= JSON.parse(localStorage.getItem('tokendata')).success;
      if (Storagetoken) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
        config.headers.Authorization = 'BasicAuth' + " " + Storagetoken;
      }
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  });

//http response 拦截器
axios.interceptors.response.use(
  response => {
      return response;
  },
  error => {
      if (error.response) {
        switch (error.response.status) {
          case 403:403
              router.replace({
                  name: 'Login',
                  query: {redirect: router.currentRoute.fullPath}
              })
      }
  }
  return Promise.reject(error.response.data)  
});



// router.beforeEach((to, from, next) => {
//   if (to.matched.some(record => record.meta.requiresAuth)) {
//     if (localStorage.getItem('tokendata')) {
//       let Storagetoken = JSON.parse(localStorage.getItem('tokendata')).token;
//       if (!Storagetoken) {
//         next({
//           name: '/login',
//           query: {
//             redirect: to.fullPath
//           }
//         });
    
//     }
//   }
//   }
//   next();
// });



/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  Mint,
  template: '<App/>',
  components: { App }
})


